package com.odm.communitymanage.ui.notifications

import android.os.Bundle
import android.view.View
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.jeremyliao.liveeventbus.LiveEventBus
import com.odm.communitymanage.R
import com.odm.communitymanage.base.BaseMVVMFragment
import com.odm.communitymanage.core.Constants
import com.odm.communitymanage.ui.CMViewPagerAdapter
import com.zhengsr.tablib.view.adapter.TabFlowAdapter
import com.zhengsr.tablib.view.flow.TabFlowLayout
import kotlinx.android.synthetic.main.fragment_notifications.*
import me.yokeyword.fragmentation.ISupportFragment


/**
 * @description: 通知页
 * @author: ODM
 * @date: 2020/4/14
 */
class NotificationsFragment : BaseMVVMFragment() {


    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
    }

    override fun initViewModelObserve() {

    }

    override fun initViews() {
        //不可以直接用Context(Activity)的FragmentManager，因为它已经被当前Fragment使用了，要用childFragmentManager给嵌套的Fragment
        vpNotifications.adapter = CMViewPagerAdapter(childFragmentManager,
                                                     mutableListOf( NoticesFragment(),SystemNotificationFragment()))
        vpNotifications.offscreenPageLimit = 2

        initTabLayout()
    }



    private fun initTabLayout() {
        val tabTitles: List<String> = mutableListOf(getString(R.string.post_notification),getString(R.string.system_notification))
        val flowLayout: TabFlowLayout = tbNotifications
        flowLayout.setViewPager(vpNotifications)
            .setTextId(R.id.item_text)
            .setSelectedColor(ColorUtils.getColor(R.color.tab_selected))
            .setUnSelectedColor(ColorUtils.getColor(R.color.tab_unselected))
        //设置数据，这里以 setAdapter 的形式
        flowLayout.adapter = object : TabFlowAdapter<String?>(R.layout.item_tab_text, tabTitles) {
            override fun onItemSelectState(view: View?, isSelected: Boolean) {
                super.onItemSelectState(view, isSelected)
                //选中时，可以改变不同颜色，如果你的background 为 selector，可以不写这个
            }

            override fun onItemClick(view: View?, data: String?, position: Int) {
                super.onItemClick(view, data, position)
                //viewPager换页
                vpNotifications.currentItem = position
            }

            override fun bindView(view: View?, data: String?, position: Int) {
                /**
                 * 绑定数据，可以使用 setText(..) 等快捷方式，也可以视同 view.findViewById()
                 * 同时，当你的子控件需要点击事件时，可以通过  addChildrenClick() 注册事件，
                 * 然后重写 onItemChildClick(..) 即可拿到事件，否则就自己写。
                 * 自己的点击和长按不需要注册
                 */
                setText(view, R.id.item_text, data).setTextColor(view, R.id.item_text, resources.getColor(R.color.tab_unselected))
                // 注册子控件的点击事件
                //addChildrenClick(view,R.id.item_text,position);
                //注册子控件的长按事件
                //addChildrenLongClick(view,R.id.item_text,position);
            }
        }
    }


    override fun shouldPopUpCloseDialog(): Boolean {
        return true
    }

    public fun startDetailFragment(){
        start(NoticeDetailFragment(), ISupportFragment.SINGLETASK)
    }


    override fun getLayoutId(): Int {
        return R.layout.fragment_notifications
    }


/*    internal class NotificationViewPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {

        private val mFragments : List<Fragment> = mutableListOf( NoticesFragment(),SystemNotificationFragment())

        override fun getItem(position: Int): Fragment {
            //获取比count要大的位置 返回最后一个Fragment
            if(position >= count){
                mFragments[count - 1]
            }
            return mFragments.get(position)
        }

        override fun getCount(): Int {
            return mFragments.size
        }
    }*/
}